<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习一</title>
  </head>

  <body>

  </body>
  <script>
    /* 
    指定日期返回今年的第几天？
    */
    //解法一：
    function getSomeDay(date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1;
      let someDate = date.getDate();
      let monthDate = 0; //记录每月有多少天
      let sumDate = 0; //记录是今年的第多少天
      for (let i = 1; i < month; i++) {
        switch (i) {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
            monthDate = 31;
            break;
          case 2:
            year % 4 === 0 ? monthDate = 29 : monthDate = 28;
            break;
          case 4:
          case 6:
          case 9:
          case 11:
            monthDate = 30;
            break;
        }
        // if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
        //   monthDate = 31;
        // } else if (i == 4 || i == 6 || i == 9 || i == 11) {
        //   monthDate = 30;
        // } else {
        //   //二月
        //   year % 4 === 0 ? monthDate = 29 : monthDate = 28
        // }
        sumDate += monthDate;
      }
      sumDate += someDate;
      return sumDate;
    }
    let date = new Date("2020-06-02");
    console.log(date.toString() + "是今年的第", getSomeDay(date) + "天"); //31+29+31+30+31+2=154 



    //解法二：时间戳相减
    /* function getCurrentDay(date) {
      let year = date.getFullYear();
      let newDate = new Date(year.toString()).getTime() //获得新年第一天八点的日期
      let unixDate = date.getTime();
      let unixCha = unixDate - newDate; //时间差

      let day = Math.ceil(unixCha / 1000 / 60 / 60 / 24) + 1;
      return day
    }
    let date = new Date("2020-06-02");
    console.log(date.toString() + "是今年的第" + getCurrentDay(date) + "天"); */
  </script>

</html>